

import gov.nih.mipav.view.ViewUserInterface;

import java.util.ArrayList;

import javax.swing.JOptionPane;


public class PropertyManager {

	private ArrayList<Property> errors;
	private ArrayList<Property> warnings;

	public PropertyManager() {
		errors = new ArrayList<Property>();
		warnings = new ArrayList<Property>();
	}

	public boolean checkProperties() {
		boolean propertiesOK = true;

		propertiesOK = errorChecking();

		if (propertiesOK)
			warningChecking();

		return propertiesOK;
	}

	private void warningChecking() {

	}

	private boolean errorChecking() {
		errors.clear();
		boolean propertiesOK = true;

		ImageManager jsImageManager = DataManager.getInstance().getImageManager();
		MaskManager jsMaskManager = DataManager.getInstance().getMaskManager();
		InitialPointsManager jsInitialPointsManager = DataManager.getInstance()
				.getInitialPointsManager();

		if (jsImageManager.getImagesNames().size() < 2) {
			Property imagesError = new Property("needMoreImages",
					Settings.ERROR_NOT_ENOUGH_IMAGES_TO_SEGMENT, "error");
			errors.add(imagesError);
		}

		if (jsImageManager.getImagesNames().contains(
				jsMaskManager.getMaskName())) {
			Property maskError = new Property("maskInImages",
					Settings.ERROR_MASK_CANNOT_BE_A_SELECTED_IMAGE, "error");
			errors.add(maskError);
		}
		if (jsImageManager.getImagesNames().contains(
				jsInitialPointsManager.getInitialPointsName())) {
			Property ipError = new Property("ipInImages",
					Settings.ERROR_INITIAL_POINTS_CANNOT_BE_A_SELECTED_IMAGE,
					"error");
			errors.add(ipError);
		}
		if (jsMaskManager.getMaskName().equals(
				jsInitialPointsManager.getInitialPointsName())) {
			Property maskIpError = new Property("maskIpError",
					Settings.ERROR_MASK_CANNOT_BE_SAME_AS_INITIAL_POINTS,
					"error");
			errors.add(maskIpError);
		}
		/*if (!checkImagesDimensions()) {
			JSProperty maskIpError = new JSProperty("dimensionError",
					Settings.ERROR_DIMENSIONS_DIFFER, "error");
			errors.add(maskIpError);
		}*/
		if (!errors.isEmpty()) {
			propertiesOK = false;
			displayErrors();
		}

		return propertiesOK;
	}

	private boolean checkImagesDimensions() {
		boolean value = true;
		JSMethodProxy proxy = JSMethodProxy.getInstance();
		value = (proxy.getImages().length == proxy.getDepth()*proxy.getHeight()*proxy.getWidth()*proxy.getNumberOfImages());
		value = value && (proxy.getMask().length == proxy.getDepth()*proxy.getHeight()*proxy.getWidth());
		value = value && (proxy.getInitialPoints().length == proxy.getDepth()*proxy.getHeight()*proxy.getWidth());
		return value;
	}

	public void displayErrors() {
		String message = "";
		for (int i = 0; i < errors.size(); i++) {
			message += errors.get(i).getMessage() + "\n";
		}
		JOptionPane.showMessageDialog(ViewUserInterface.getReference()
				.getMainFrame(), message, "Error", JOptionPane.ERROR_MESSAGE);

	}

	public void displayWarnings() {

	}
}
